package com.bear.fastrepair.mvvm.ui.manager

import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import com.bear.fastrepair.R
import com.bear.fastrepair.base.BaseFragment
import com.bear.fastrepair.databinding.FragmentManagerOrderBinding
import com.bear.fastrepair.entity.OrderRecord
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils

class ManagerOrderFragment : BaseFragment<FragmentManagerOrderBinding>() {
    private lateinit var viewModel: ManagerViewModel
    private val data = ArrayList<OrderRecord>()

    override val title: String
        get() = getString(R.string.order_center)

    override fun init() {
        viewModel = ViewModelProvider(
            requireActivity(),
            ManagerViewModelFactory()
        ).get(ManagerViewModel::class.java)
        viewModel.getOrderInfoPage(1, 100)
        viewModel.getOrderInfoPage.observe(viewLifecycleOwner) {
            if (it.code != 0) {
                LogUtils.i("error msg is ${it.msg}")
                if (it.code == 10002) {
                    ToastUtils.showLong("${it.msg} 请重新登录")
                    findNavController().navigate(R.id.loginFragment)
                }
                return@observe
            }
            data.addAll(it.data.records)
            val adapter = OrderAdapter(data)
            binding.rvOrder.let { rv ->
                rv.layoutManager = LinearLayoutManager(requireContext())
                rv.adapter = adapter
            }
        }
        binding.toolbar.toolbarTitle.text = title
    }
}